/**
 * 开票通知
 * @author liuw
 * @date 2019/09/04
 */
define(function () {
    var self,
        doc = document,
        app = win.app,
        layui = win.layui,
        element,
        table,
        table_param = {},
        cols = [[
            {type:'checkbox',fixed: 'left'},
            { field: "GWEDDOCNO", title: "申请单号", width: 150,sort:true,search:'default',searchOpts:{icon:true}},
            { field: "GWEDSEQ", title: "项次", width: 50},
            { field: "GWEDDOCDATE", title: "申请日期", width: 100,search:'default',searchOpts:{type:'date'},sort:true},
            { field: "GWED001", title: "申请人", width: 100,hide:true,
                search:'default',searchOpts:{icon:true},},
            { field: "GWED001_NAME", title: "申请人", width: 100},
            { field: "GWED002", title: "发票种类", width: 100,filterDatas: app.datas_('crm_gwei006_gwed002')},
            { field: "GWED003", title: "出货单号", width: 150,sort:true},
            { field: "GWED004", title: "客户", hide:true,
                search:'default',searchOpts:{icon:true}},
            { field: "GWED004_NAME", title: "客户", width: 100},
            { field: "GWED008", title: "产品编码", width: 100},
            { field: "GWAML003", title: "品名", width: 200},
            { field: "GWAML004", title: "规格", width: 100},
            { field: "GWED009", title: "数量", width: 100,sort:true},
            { field: "GWED010", title: "单价", width: 100,sort:true},
            { field: "GWED011", title: "金额", width: 100,sort:true},
            { field: "GWED013", title: "备注", width: 100},
            { field: "GWEDCONF", title: "状态", width: 100,
                filterDatas: app.datas_('crm_gwcd_gwcdacti'),search: 'default', searchOpts:{type:'select'}},
            { field: "GWEDACTI", title: "有效否", width: 100,filterDatas: app.datas_('sys_y_n')},
            {
                width: 60,
                title: '',
                toolbar: '#script-gwei006setting',
                fixed: 'right',
                align: 'center',
                unresize: true
            }
        ]],
        gwei006SettingAction,
        moduleName = '开票通知'
    ;

    app.newModule({
        init: function () {
            self = this;
            self._super({
                _tag: 'gwei006',
                _searchs: [
                    { id: 'gwei006', tableId: 'gwei006_table',
                        datas:  cols[0]
                    }
                ],
                _loadTplsFull: [{
                    dom: 'gwei006_edit', tpl: 'gwei006_edit'
                },{
                    dom: 'common_open_window', tpl: 'common/_open_window'
                },{
                    dom: 'gwei006_print', tpl: 'gwei006_print'
                }]
            }, win);
        },

        initTable: function () {
            layui.use(['table'], function () {
                table = layui.table;
                table.render({
                    elem: app.getByid(doc, 'gwei006_table'),
                    url: g_ctxPath + 'crm/gwei006/getList',
                    exportsUrl: g_ctxPath + 'crm/gwei006/getGwcslist',
                    exportsName: moduleName + '数据',
                    title: moduleName + '信息表',
                    method: 'post',
                    id: 'gwei006_table',
                    toolbar: '#table-toolbar',
                    defaultToolbar: ['filter'],
                    height: 'full',
                    cols: cols,
                    loading: true,
                    page: true,
                    where: ''
                });

                layui.laytpl(app.getByid(doc, 'script-gwei006setting-action').innerHTML).render({}, function (string) {
                    gwei006SettingAction = string;
                });

                var tipsIndex = null;

                table.on('tool(gwei006_table)', function (obj) {

                    layer.tips(gwei006SettingAction, obj.td, {
                        tips: [3, '']
                        , time: 3000
                        , anim: 500
                        , maxWidth: 120
                        , skin: 'layui-table-tips'
                        , success: function (layero, index) {
                            layero.find('li').on('click', function () {
                                switch ($(this).attr('inx')) {
                                    case '0':
                                        self._openTplFull({
                                            tag: 'gwei006_edit',
                                            tagData:{
                                                moduleName:moduleName,
                                                obj:obj,
                                                is_add:true
                                            },
                                            index: 0,
                                            openOpts:{
                                                title: moduleName + '信息编辑',
                                                minmax:true
                                            }
                                        });
                                        break;
                                    case '1':
                                        if(obj.data.GWEDCONF === 'Y' ){
                                            app.showWarning("已审核的数据不能删除");
                                            return ;
                                        }
                                        self.delData(obj);
                                        break;
                                }
                                layer.close(index);
                            });
                        }
                    });
                });

                //监听行双击事件
                table.on('rowDouble(gwei006_table)', function (obj) {
                    self._openTplFull({
                        tag: 'gwei006_edit',
                        tagData: {
                            moduleName:moduleName,
                            obj:obj,
                            is_add:false
                        },
                        index: 0,
                        openOpts: {
                            title: moduleName + '详情',
                            minmax:true
                        }
                    });
                });

                //监听事件
                table.on('toolbar(gwei006_table)', function (obj) {
                    switch (obj.event) {
                        case 'add':
                            self._openTplFull({
                                tag: 'gwei006_edit',
                                tagData: {
                                    is_add: true
                                },
                                index: 0,
                                openOpts: {
                                    title: moduleName + '信息编辑',
                                    minmax:true
                                }
                            });
                            break;
                        case 'del':
                            self.delDataBatch();
                            break;
                        case 'check':
                            self.checkDataBatch();
                            break;
                        case 'unCheck':
                            self.unCheckDataBatch();
                            break;
                        case 'print':
                            self.print();
                            break;
                    }
                });

                $(doc).on('click', function () {
                    tipsIndex && layer.close(tipsIndex);
                });

            });
        },

        reloadTableCurr: function () {
            self._reloadTable({id: 'gwei006_table', where: table_param});
        },

        //打印
        print:function(){
            var checkStatus = table.checkStatus('gwei006_table');
            if(checkStatus.data.length>0){

                if(checkStatus.data.length > 1){
                    app.showMessage('操作被取消，该业务只能操作单条记录，请重新选择。')
                    return;
                }
                app.pageModule('gwei006_print').print(checkStatus.data[0].GWEDDOCNO);

            }else{
                app.showMessage('请先选择待操作的记录')
            }
        },

        delData: function (obj) {
            if(obj.data.GWEDCONF == 'Y') {
                app.showWarning("已审核的数据无法删除！");
                return;
            }
            app.ajax({
                xurl: 'crm/gwei006/del',
                xdata: obj.data,
                loading: true,
                confirm: {msg: "是否删除选择的数据？"},
                okmsg: moduleName + '删除成功',
                cb: function (result) {
                    if (result.ok) {
                        self.reloadTableCurr();
                    }
                }
            });

        },

        delDataBatch: function(){
            var checkStatus = table.checkStatus('gwei006_table');

            var flag = true;
            if (checkStatus.data.length > 0) {
                var _xdata = [];
                checkStatus.data.forEach(function (v) {
                    var _temp = {};
                    if(v.GWEDCONF == 'Y' ){
                        app.showWarning("存在已审核的数据，无法删除！");
                        flag = false;
                        return ;
                    }
                    _temp.GWEDDOCNO = v.GWEDDOCNO;
                    _temp.GWEDSEQ = v.GWEDSEQ;
                    _xdata.push(_temp);
                });
                if(flag){
                    app.ajax({
                        xurl: 'crm/gwei006/delDataBatch',
                        xdata: _xdata,
                        loading: true,
                        toJsonStr: true,
                        confirm: {msg: "是否删除选择的数据？"},
                        okmsg: moduleName + '信息删除成功',
                        cb: function (result) {
                            if (result.ok) {
                                self.reloadTableCurr();
                            }
                        }
                    });
                }
            } else {
                app.showWarning("至少选择一条数据进行操作");
            }
        },

        checkDataBatch: function(){
            var checkStatus = table.checkStatus('gwei006_table');
            var flag = true;
            if (checkStatus.data.length > 0) {
                var _xdata = [];
                checkStatus.data.forEach(function (v) {
                    var _temp = {};
                    if(v.GWEDCONF == 'Y' ){
                        app.showWarning("存在已审核的数据，未审核的数据才能审核！");
                        flag = false;
                        return ;
                    }
                    _temp.GWEDDOCNO = v.GWEDDOCNO;
                    _temp.GWEDSEQ = v.GWEDSEQ;
                    _xdata.push(_temp);
                });
                if(flag){
                    app.ajax({
                        xurl: 'crm/gwei006/checkDataBatch',
                        xdata: _xdata,
                        loading: true,
                        toJsonStr: true,
                        confirm: {msg: "是否审核选择的数据？"},
                        okmsg: moduleName + '信息审核成功',
                        cb: function (result) {
                            if (result.ok) {
                                self.reloadTableCurr();
                            }
                        }
                    });
                }
            } else {
                app.showWarning("至少选择一条数据进行操作");
            }
        },

        unCheckDataBatch: function(){
            var checkStatus = table.checkStatus('gwei006_table');
            var flag = true;
            if (checkStatus.data.length > 0) {
                var _xdata = [];
                checkStatus.data.forEach(function (v) {
                    var _temp = {};
                    if(v.GWEDCONF == 'N' ){
                        app.showWarning("存在未审核的数据，已审核的数据才能取消审核！");
                        flag = false;
                        return ;
                    }
                    _temp.GWEDDOCNO = v.GWEDDOCNO;
                    _temp.GWEDSEQ = v.GWEDSEQ;
                    _xdata.push(_temp);
                });
                if(flag){
                    app.ajax({
                        xurl: 'crm/gwei006/cancelCheckDataBatch',
                        xdata: _xdata,
                        loading: true,
                        toJsonStr: true,
                        confirm: {msg: "是否取消审核选择的数据？"},
                        okmsg: moduleName + '信息取消审核成功',
                        cb: function (result) {
                            if (result.ok) {
                                self.reloadTableCurr();
                            }
                        }
                    });
                }
            } else {
                app.showWarning("至少选择一条数据进行操作");
            }
        },

        _ready: function () {
            this._super({
                vue: [{
                    el: '#page',
                    data: '',
                    searchId: 'gwei006',
                    methods: {
                        handleIconClick: function (field) {
                            var vue = this;
                            if (field == 'GWED001') {//申请人员开窗
                                self._openTplFull({
                                    tag: 'common_open_window',
                                    tagData: {
                                        config: {
                                            method:'q_sysuser_id',
                                            columnList: [
                                                {label: '申请人员编号', prop: 'ID', width: '150'},
                                                {label: '申请人员名称', prop: 'NAME', width: '150'}
                                            ],
                                        },
                                        cb: function (data) {
                                            vue.isearch.fields.GWED001 = data.ID;
                                        }
                                    },
                                    index: 1,
                                    openOpts: {
                                        title: '选择申请人员',
                                        area: ['600px', '465px']
                                    }
                                });
                            } else if (field == 'GWEDDOCNO') {//单号开窗
                                self._openTplFull({
                                    tag: 'common_open_window',
                                    tagData: {
                                        config: {
                                            method:'q_gweddocno',
                                            columnList: [
                                                {label: '申请单号', prop: 'GWEDDOCNO', width: '150'},
                                                {label: '申请日期', prop: 'GWEDDOCDATE', width: '150'},
                                                {label: '申请人', prop: 'GWED001', width: '150'},
                                                {label: '状态', prop: 'GWEDCONF', width: '150',sortable:false}
                                            ],
                                        },
                                        cb: function (data) {
                                            vue.isearch.fields.GWEDDOCNO = data.GWEDDOCNO;
                                        }
                                    },
                                    index: 1,
                                    openOpts: {
                                        title: '选择申请单号',
                                        area: ['630px', '465px']
                                    }
                                });
                            } else if (field == 'GWED004') {//客户开窗
                                self._openTplFull({
                                    tag: 'common_open_window',
                                    tagData: {
                                        config: {
                                            method:'q_gwak001',
                                            columnList: [
                                                {label:'客户编号',prop:'GWAK001', width: 150},
                                                {label:'客户简称',prop:'GWAKL003', width: 150},
                                                {label:'客户全称',prop:'GWAKL004', width: 150}
                                            ],
                                        },
                                        cb: function (data) {
                                            vue.isearch.fields.GWED004 = data.GWAK001;
                                        }
                                    },
                                    index: 1,
                                    openOpts: {
                                        title: '选择客户',
                                        area: ['600px', '465px']
                                    }
                                });
                            }
                        }
                    }
                }]
            });
            self.initTable();
        },

        _lazy: function () {
            this._super();
            layui.use(['table'], function () {
                element = layui.element;
            });
        }
    });

});